import { IsEmpty } from 'class-validator';
import { ApiProperty, getSchemaPath } from '@nestjs/swagger';
import { CrudValidationGroups } from '@nestjsx/crud';
import { IsNumber, IsOptional, IsString, ValidateNested } from 'class-validator';
import {
  Column,
  Entity,
  ManyToOne,
  PrimaryGeneratedColumn,
  OneToOne,
  JoinColumn,
  IsNull
} from 'typeorm';
import { file } from './about/file';
import { BaseEntity } from './base.entity';
import { EduEnum } from './enum/edu.enum';
import { PoliticalStatusEnum } from './enum/political-status.enum';
import { Group } from './group.entity';
import { Village } from './village.entity';
import { VillagerInfo } from './villager-info.entity';
import { SexEnum } from './enum/sex.enum';
import { NationEnum } from './enum/nation.enum';

const { CREATE, UPDATE } = CrudValidationGroups;

@Entity()
export class PartyMember extends BaseEntity {
  @ApiProperty({
    description: '党员主键',
    required: false,
  })
  @PrimaryGeneratedColumn()
  @IsOptional()
  id?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number',
      },
      {
        $ref: getSchemaPath(() => Village),
      },
    ],
  })
  @ManyToOne(
    () => Village,
    Village => Village.partyMember,
    {
      cascade: true,
    },
  )
  village: Number | Village;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
  })
  @ManyToOne(
    () => Group,
    Group => Group.partyMember,
    {
      eager: true,
    },
  )
  group: Number | Group;


  @ApiProperty({
    example: '党组织',
    description: '党组织',
    required: true,
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  partyOrganization: string;

  // @ApiProperty({
  //   example: 1,
  //   description: '村民信息',
  //   required: true,
  // })
  // @ValidateNested({ always: true })
  // @OneToOne(type => VillagerInfo)
  // @JoinColumn()
  // villagerInfo: VillagerInfo;

  @ApiProperty({
    example: 1,
    description: '身份状态',
    required: true,
    type: Number,
    enum: PoliticalStatusEnum,
  })
  @IsNumber({}, { always: true })
  @Column({
    type: "enum",
    enum: PoliticalStatusEnum,
  })
  status: PoliticalStatusEnum;

  @ApiProperty({
    example: '职务',
    description: '职务',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    nullable: true,
    type: "text",
  })
  duties: string;

  @ApiProperty({
    example: '就职单位',
    description: '就职单位',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "text",
    nullable: true
  })
  lnauguralAddress: string;

  @ApiProperty({
    example: '文化程度',
    description: '文化程度',
    required: false,
    type: Number,
    enum: EduEnum
  })
  @IsNumber({}, { always: true })
  @IsOptional({ always: true })
  @Column({
    type: "enum",
    enum: EduEnum,
    nullable: true
  })
  education: EduEnum;

  @ApiProperty({
    example: '积极分子时间',
    description: '积极分子时间',
    required: true,
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  periodOfActivists: string;

  @ApiProperty({
    example: '发展对象时间',
    description: '发展对象时间',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true,
  })
  periodOfDevelopmentObject: string;

  @ApiProperty({
    example: '预备党员时间',
    description: '预备党员时间',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true,
  })
  periodOfProbationaryMember: string;

  @ApiProperty({
    example: '正式党员时间',
    description: '正式党员时间',
    required: true,
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true,
  })
  periodOfPartyMember: string;

  @ApiProperty({
    example: '照片',
    description: '照片',
    required: true,
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  image: file[];

  @ApiProperty({
    example: '名字',
    description: '名字',
    required: true,
  })
  @IsString({ always: true })
  @Column({
    // nullable: true,
    name: "name",
    type: "char",
    length: 255,
  })
  name: string;

  @ApiProperty({
    example: SexEnum.men,
    description: '性别',
    required: true,
    type: Number,
    enum: SexEnum
  })
  @IsNumber({}, { always: true })
  @Column({
    name: "sex",
    type: "enum",
    enum: SexEnum,
  })
  sex: SexEnum;

  @ApiProperty({
    example: NationEnum.HAN,
    description: '民族',
    required: true,
    type: Number,
    enum: NationEnum
  })
  @IsNumber({}, { always: true })
  @IsOptional()
  @Column({
    nullable: true,
    name: "nation",
    type: "enum",
    enum: NationEnum,
  })
  nation?: NationEnum;

  @ApiProperty({
    example: 176666666666,
    description: '电话',
    required: false,
  })
  @IsString()
  @IsOptional()
  @Column({
    nullable: true,
    name: "phone",
  })
  phone?: string;

  @ApiProperty({
    example: 5555555555,
    description: '身份证号',
    required: true,
  })
  @IsString()
  @IsOptional()
  @Column({
    nullable: true,
    name: "identityNumber",
    type: "char",
    length: 255,
  })
  identityNumber?: string;

  @ApiProperty({
    example: '地址',
    description: '地址',
    required: true,
  })
  @IsString()
  @IsOptional()
  @Column({
    nullable: true,
    name: "address",
    type: "char",
    length: 255,
  })
  address?: string;
}
